home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STEP14.PAK / STEP14.CPP next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  156 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1994 by Borland International
  3. //   Tutorial application -- step14.cpp
  4. //   OLE 2 container example
  5. //----------------------------------------------------------------------------
  6. #include <owl/pch.h>
  7. #include <owl/applicat.h>
  8. #include <owl/dialog.h>
  9. #include <owl/controlb.h>
  10. #include <owl/buttonga.h>
  11. #include <owl/statusba.h>
  12. #include <owl/docmanag.h>
  13. #include <owl/olemdifr.h>
  14. #include <owl/oleview.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "step14.rc"
  18.  
  19. DEFINE_APP_DICTIONARY(AppDictionary);
  20. static TPointer<TOcRegistrar> Registrar;
  21.  
  22. // Registration
  23. //
  24. REGISTRATION_FORMAT_BUFFER(100)
  25.  
  26. BEGIN_REGISTRATION(AppReg)
  27.   REGDATA(clsid,      "{5E4BD310-8ABC-101B-A23B-CE4E85D07ED2}")
  28.   REGDATA(appname,    "DrawPad Container")
  29. END_REGISTRATION
  30.  
  31. //
  32. // TDrawApp
  33. //
  34. class _USERCLASS TDrawApp : public TApplication, public TOcModule {
  35.   public:
  36.     TDrawApp() : TApplication(::AppReg["appname"]) {}
  37.  
  38.   protected:
  39.     TMDIClient*      Client;
  40.  
  41.     // Override methods of TApplication
  42.     void InitInstance();
  43.     void InitMainWindow();
  44.  
  45.     // Event handlers
  46.     void EvNewView(TView& view);
  47.     void EvCloseView(TView& view);
  48.     void EvDropFiles(TDropInfo dropInfo);
  49.     void CmAbout();
  50.  
  51.   DECLARE_RESPONSE_TABLE(TDrawApp);
  52. };
  53.  
  54. DEFINE_RESPONSE_TABLE1(TDrawApp, TApplication)
  55.   EV_OWLVIEW(dnCreate, EvNewView),
  56.   EV_OWLVIEW(dnClose,  EvCloseView),
  57.   EV_WM_DROPFILES,
  58.   EV_COMMAND(CM_ABOUT, CmAbout),
  59. END_RESPONSE_TABLE;
  60.  
  61. void
  62. TDrawApp::InitMainWindow()
  63. {
  64.   TOleMDIFrame* frame;
  65.   frame = new TOleMDIFrame(GetName(), 0, *(Client = new TMDIClient), true);
  66.  
  67.   // Construct a status bar
  68.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed);
  69.  
  70.   // Construct a control bar
  71.   TControlBar* cb = new TControlBar(frame);
  72.   cb->Insert(*new TButtonGadget(CM_FILENEW, CM_FILENEW, TButtonGadget::Command));
  73.   cb->Insert(*new TButtonGadget(CM_FILEOPEN, CM_FILEOPEN, TButtonGadget::Command));
  74.   cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE, TButtonGadget::Command));
  75.   cb->Insert(*new TButtonGadget(CM_FILESAVEAS, CM_FILESAVEAS, TButtonGadget::Command));
  76.   cb->Insert(*new TSeparatorGadget);
  77.   cb->Insert(*new TButtonGadget(CM_PENSIZE, CM_PENSIZE, TButtonGadget::Command));
  78.   cb->Insert(*new TButtonGadget(CM_PENCOLOR, CM_PENCOLOR, TButtonGadget::Command));
  79.   cb->Insert(*new TSeparatorGadget);
  80.   cb->Insert(*new TButtonGadget(CM_ABOUT, CM_ABOUT, TButtonGadget::Command));
  81.   cb->SetHintMode(TGadgetWindow::EnterHints);
  82.  
  83.   cb->Attr.Id = IDW_TOOLBAR;
  84.  
  85.   // Insert the status bar and control bar into the frame
  86.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  87.   frame->Insert(*cb, TDecoratedFrame::Top);
  88.  
  89.   // Set the main window and its menu
  90.   SetMainWindow(frame);
  91.   GetMainWindow()->SetMenuDescr(TMenuDescr(IDM_MDICMNDS));
  92.  
  93.   // Install the document manager
  94.   SetDocManager(new TDocManager(dmMDI | dmMenu));
  95. }
  96.  
  97. void
  98. TDrawApp::InitInstance()
  99. {
  100.   TApplication::InitInstance();
  101.   GetMainWindow()->DragAcceptFiles(true);
  102. }
  103.  
  104. void
  105. TDrawApp::EvDropFiles(TDropInfo dropInfo)
  106. {
  107.   int fileCount = dropInfo.DragQueryFileCount();
  108.   for (int index = 0; index < fileCount; index++) {
  109.     int fileLength = dropInfo.DragQueryFileNameLen(index)+1;
  110.     char* filePath = new char [fileLength];
  111.     dropInfo.DragQueryFile(index, filePath, fileLength);
  112.     TDocTemplate* tpl = GetDocManager()->MatchTemplate(filePath);
  113.     GetDocManager()->CreateDoc(tpl, filePath);
  114.     delete filePath;
  115.   }
  116.   dropInfo.DragFinish();
  117. }
  118.  
  119. void
  120. TDrawApp::EvNewView(TView& view)
  121. {
  122.   TMDIChild* child = new TMDIChild(*Client, 0, view.GetWindow());
  123.   if (view.GetViewMenu())
  124.     child->SetMenuDescr(*view.GetViewMenu());
  125.   child->Create();
  126. }
  127.  
  128. void
  129. TDrawApp::EvCloseView(TView& /*view*/)
  130. {
  131.   // nothing needs to be done here for MDI
  132. }
  133.  
  134. void
  135. TDrawApp::CmAbout()
  136. {
  137.   TDialog(GetMainWindow(), IDD_ABOUT).Execute();
  138. }
  139.  
  140. int
  141. OwlMain(int /*argc*/, char* /*argv*/ [])
  142. {
  143.   try {
  144.     Registrar = new TOcRegistrar(AppReg, TOleDocViewFactory<TDrawApp>(),
  145.                                  TApplication::GetCmdLine(), ::DocTemplateStaticHead);
  146.     if (Registrar->IsOptionSet(amAnyRegOption))
  147.       return 0;
  148.  
  149.     return Registrar->Run();
  150.   }
  151.   catch (xmsg& x) {
  152.     ::MessageBox(0, x.why().c_str(), "Exception", MB_OK);
  153.   }
  154.   return -1;
  155. }
  156.